package com.ruoyi.system.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import com.ruoyi.system.domain.UserIssues;

@Component
public class MailService {

    @Value("${spring.mail.username}") // 从配置文件中获取邮箱
    private String fromEmail;

    private final JavaMailSender mailSender;

    public MailService(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    /**
     * 发送简单邮件
     *
     * @param to      收件人邮箱
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendSimpleMail(String to, String subject, String content) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(fromEmail); // 设置发件人邮箱
            message.setTo(to);          // 设置收件人邮箱
            message.setSubject(subject); // 设置邮件主题
            message.setText(content);    // 设置邮件内容
            mailSender.send(message);
            System.out.println("邮件发送成功！");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("邮件发送失败：" + e.getMessage());
        }
    }
    public void sendSimpleMail(String email, String image) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(fromEmail);
            message.setTo(email); 
            message.setSubject("邮箱注册验证");
            System.out.println(image);
            message.setText("您的验证码图片如下：\n <img src=\"data:image/gif;base64,"+ image +"\"></img>");
            mailSender.send(message);
        } catch (Exception e) {
                e.printStackTrace();
                System.out.println("邮件发送失败：" + e.getMessage());
            }


    }
    public void sendUserIssues(UserIssues userIssues) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(fromEmail);
            // message.setTo("1094703137@qq.com");
            message.setTo("2221838436@qq.com");
            message.setSubject("用户投诉："+ userIssues.getTitle());
            message.setText("投诉内容如下：\n 用户ID："+ userIssues.getUserId() + "\n标题："+ userIssues.getTitle() + "\n内容："+ userIssues.getRichContent()  + "\n创建时间："+ userIssues.getCreatedAt() );
            mailSender.send(message);
        } catch (Exception e) {
                e.printStackTrace();
                System.out.println("邮件发送失败：" + e.getMessage());
            }


    }
}
